<template>
  <el-aside
    width="200px"
    class="image-aside"
    v-loading="loading"
  >
    <div class="top">
      <AsideList
        v-for="(item, index) in list"
        :key="index"
        :active="activeId == item.id"
        @edit="handleEdit(item)"
        @delete="handleDelete(item.id)"
        @click="handleChangeActiveId(item.id)"
      >
        {{ item.name }}
      </AsideList>
    </div>

    <div class="bottom">
      <el-pagination
        background
        layout="prev, next"
        :total="total"
        :current-page="currentPage"
        :page-size="limit"
        @current-change="getData"
      />
    </div>
  </el-aside>
  <FormDrawer
    ref="formDrawerRef"
    :title="drawerTitle"
    @submit="handleSubmit"
  >
    <el-form
      :model="form"
      ref="formRef"
      :rules="rules"
      label-width="80px"
      :inline="false"
    >
      <el-form-item
        label="分类名称"
        prop="name"
      >
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item
        label="排序"
        prop="order"
      >
        <el-input-number
          v-model="form.order"
          :min="0"
          :max="1000"
        />
      </el-form-item>
    </el-form>
  </FormDrawer>
</template>

<script setup>
import { ref, reactive, computed } from "vue";
import AsideList from "./AsideList.vue";
import FormDrawer from "./FormDrawer.vue";
import {
  getImageClassList, // 获取图库分类列表
  createImageClass, // 新增图库分类
  updateImageClass, // 修改图库分类
  deleteImageClass, // 删除图库分类
} from "~/api/image_class.js";

import { toast } from "~/composables/util.js";

// 加载动画
const loading = ref(false);
const list = ref([]);

// 当前页码
const currentPage = ref(1);
// 数据总条数
const total = ref(0);
//  每页显示多少条数据
const limit = ref(10);

// 图库分类id
const editId = ref(0);
const drawerTitle = computed(() => (editId.value ? "修改" : "新增"));

// 获取数据
function getData(p = null) {
  // 获取的是页码
  console.log("current-change:", p);
  if (typeof p == "number") {
    currentPage.value = p;
  }
  loading.value = true;
  getImageClassList(currentPage.value) // 获取第一页
    .then((res) => {
      console.log("getImageClassList:", res);
      total.value = res.totalCount;
      list.value = res.list;
      // 激活状态默认选中第一个
      let item = list.value[0];
      if (item) {
        handleChangeActiveId(item.id);
      }
    })
    .finally(() => {
      loading.value = false;
    });
}

getData();

const formDrawerRef = ref(null);
// 打开抽屉
const handleCreate = () => {
  // 新增时，清空数据
  form.name = "";
  form.order = 50;
  editId.value = 0;
  formDrawerRef.value.open();
};

const form = reactive({
  name: "",
  order: 50,
});

const rules = {
  name: [
    {
      required: true,
      message: "图库分类名称不能为空",
      trigger: "blur",
    },
  ],
};

const formRef = ref(null);

const handleSubmit = () => {
  console.log("提交表单");
  formRef.value.validate((valid) => {
    if (!valid) return;
    console.log("提交成功！");
    // 这里的form就是：
    // const form=reactive({
    //   name:'',
    //   order:50
    // })
    formDrawerRef.value.showLoading();
    const fun = editId.value
      ? updateImageClass(editId.value, form)
      : createImageClass(form);

    fun.then((res) => {
      toast(drawerTitle.value + "成功");
      // 新增时，获取第一页数据，修改就是获取当前页数据
      getData(editId.value ? currentPage.value : 1);
      // 关闭弹窗
      formDrawerRef.value.close();
      formDrawerRef.value.hideLoading();
    });
  });
};

// 选中图库分类ID
// 记录激活状态
const activeId = ref(0);
const emit = defineEmits(["change"]);
// 切换分类
function handleChangeActiveId(id) {
  activeId.value = id;
  console.log("handleChangeActiveId:", id);
  // 点击分类的时候，先通知父组件，父组件去调用ImageAside组件的方法
  emit("change", id);
}

defineExpose({
  handleCreate,
});

// 修改图库分类
const handleEdit = (row) => {
  console.log(row);
  editId.value = row.id;
  // 回显表单数据
  form.name = row.name;
  form.order = row.order;
  formDrawerRef.value.open();
};

// 删除图库分类
const handleDelete = (id) => {
  loading.value = true;
  deleteImageClass(id)
    .then((res) => {
      toast("删除成功");
      getData();
    })
    .finally(() => {
      loading.value = false;
    });
};
</script>

<style>
.image-aside {
  border-right: 1px solid #eee;
  position: relative;
}
.image-aside .top {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 50px;
  overflow-y: auto;
}

.image-aside .bottom {
  position: absolute;
  bottom: 0;
  height: 50px;
  left: 0;
  right: 0;
  @apply flex items-center justify-center;
}

:v-deep(.el-form-item) {
  margin-bottom: 18px !important;
}
</style>
